package com.printeverywhere.delegator;

import static com.printeverywhere.util.AdminConstants.ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.SESSION_CLOSED;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.printeverywhere.dto.Currency;
import com.printeverywhere.dto.CurrencyConverterConfiguration;
import com.printeverywhere.dto.Feedback;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.util.HibernateUtil;

public class CurrencyServiceDelegator {
	private static Logger logger = Logger.getLogger(CurrencyServiceDelegator.class);
	/**
	 * Gets the list of all the records from GeneralCategories table without
	 * checking any predefined criteria, It simply list all the rows from
	 * database table.
	 *
	 * @return the list
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static List<Currency> getList() throws HibernateException {
		logger.info(GET_LIST_WITHOUT_SEARCH_CRITERIA);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			List<Currency> list = session.createCriteria(
					Currency.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			logger.error(ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA
					+ e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

	public static CurrencyConverterConfiguration getConfigurationList() throws HibernateException {
		logger.info(GET_LIST_WITHOUT_SEARCH_CRITERIA);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			List<CurrencyConverterConfiguration> list = session.createCriteria(CurrencyConverterConfiguration.class).list();
			transaction.commit();
			if(list.size()>0)
			return list.get(0);
			else return null;
		} catch (HibernateException e) {
			logger.error(ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA + e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

	public static void updateConfiguration(CurrencyConverterConfiguration configuration) throws HibernateException {
		if(logger.isInfoEnabled()){
			logger.info("Called updateConfiguration method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(configuration);
			tx.commit();
		} catch (RuntimeException e) {
			logger.error("Got exception when update Configuration".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(logger.isInfoEnabled()){
				logger.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}
}
